package com.tengfei.blogsystem.service.impl;

import com.tengfei.blogsystem.pojo.User;
import com.tengfei.blogsystem.utils.Constants;
import com.tengfei.blogsystem.utils.CookieUtils;
import com.tengfei.blogsystem.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-19-21:14
 * @description
 */
@Service("permission")
public class PermissionServiceImpl {

    @Autowired
    UserServiceImpl userService;

    public boolean admin() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        String tokenKey = CookieUtils.getCookie(request, "blog_login_token");
        if (TextUtils.isEmpty(tokenKey)) {
            return false;
        }

        User user = userService.checkUser(request, response);
        if (user == null) {
            return false;
        }
        if (Constants.User.ROLE_ADMIN.equals(user.getRoles())) {
            return true;
        }
        return false;
    }
}
